<?php

namespace App\Logics;


use App\Models\CourseSign;
use App\Models\Student;
use App\Models\User;
use App\Service\Base\Logic;


class CourseSignLogic extends Logic
{

    // 课程签到
    public function initialize()
    {
        parent::initialize();

        $this->model = '';
    }

    // 请假操作
    // 签到操作
    // 签到记录

    // 列表
    // 添加
    // 修改

    // 列表查询
    public function onLists($myUid = 0)
    {
        $option = $this->option();
        $page   = $option->getData('page', 1, 'intval');
        $rows   = $option->getData('rows', 15, 'intval');
        $start  = ($page - 1) * $rows;

        // 模型 和 查询
        $where  = [];
        $bang   = [];
        $c_id   = $option->getData('course_id', 0, 'intval');
        $uid    = $option->getData('uid', 0, 'intval');
        $s_id   = $option->getData('student_id', 0, 'intval');
        $s_time = $option->getData('start_time', 0, 'intval');
        $e_time = $option->getData('end_time', 0, 'intval');
        if($c_id){
            $bang[]  = 'course_id';
            $where[] = ['course_id', '=', $c_id];
        }
        if($uid){
            $bang[]  = 'uid';
            $where[] = ['uid', '=', $uid];
        }
        if($s_id){
            $bang[]  = 'student_id';
            $where[] = ['student_id', '=', $s_id];
        }
        if($s_time){
            $bang[]  = 'ctime';
            $where[] = ['ctime', '>=', $s_time];
        }
        if($e_time){
            $bang[]  = 'ctime';
            $where[] = ['ctime', '<=', $e_time];
        }

        // 模型
        $model  = new CourseSign();
        $KCache = $model->cacheKey('lists', $where);
        $lists  = $model->cacheClass()->getCache($KCache);
        // 检查是否已经绑定
        if(!empty($lists)){
            $model->checkBangList($lists, $KCache);
        }
        if(empty($lists)){
            //$result = $model->bangField($bang)->listPage($where, $page, $rows, "id DESC");
            $lists = $model->bangField($bang)->getList($where, $rows, "id DESC", $start);
            $lists = is_object($lists) ? $lists->toArray() : $lists;
            if($where != false){
                $model->addCacheList($lists, $KCache, $lists, 'list');
            }
        }

        return $lists;
    }

    // 改变（直接修改，软删除，改变状态，排序）
    // TODO 签到需要判断是否已经签到请假
    public function _onEdit($myUid)
    {
        $_id = $this->id();
        if(empty($_id)){
            return $this->_returnFail('请指定修改的ID编号！');
        }

        $opt = $this->option();
        // 获取认证编号
        $user = (new User())->isCache(true)->oneFind(['id' => $myUid]);
        if(empty($user)){
            return $this->_returnFail('你选择的用户不存在！');
        }
        if(empty($user['is_auth'])){
            return $this->_returnFail('你不是认证用户！');
        }
        if($user['verify'] != 1){
            return $this->_returnFail('你选择的用户认证未通过！');
        }

        $auth_id = $user['auth_id'];
        $data    = [
            //'id'         => $opt->getData("id", null, "intval"),
            //'uid'        => $opt->getData("uid", null, "intval"),
            'uid'        => $myUid,
            'course_id'  => $opt->getData("course_id", null, "intval"),
            'time_id'    => $opt->getData("time_id", null, "intval"),
            'student_id' => $opt->getData("student_id", null, "intval"),
            'course_uid' => $opt->getData("course_uid", null, "intval"),
            'up_time'    => $opt->getData("up_time", null, "intval"),
            'status'     => $opt->getData("status", null, "trim"),
            'is_up'      => $opt->getData("is_up", null, "trim"),
            //'ctime'      => $opt->getData("ctime", null, "intval"),
            //'mtime'      => $opt->getData("mtime", null, "intval"),
        ];

        $data = $this->filterNull($data);
        if(empty($data)){
            return $this->_returnFail('请添加需要保存的数据！');
        }

        // 组合数据
        $where = ['id' => $_id, 'uid' => $myUid];
        $model = new CourseSign();
        $data  = $model->checkData($data, $where);
        if($data === false){
            return $this->_returnFail('请添加需要保存的数据！');
        }

        // 验证
        $result = $model->checkRules($data, 'edit');
        if($result === false){
            return $this->_returnFail($model->getError());
        }

        // 保存
        $result = $model->oneEdit($where, $data);
        if($result === false){
            return $this->_returnFail($model->getError());
        }

        return $result;
    }

    // 添加（单个签到或者请假）
    // TODO 签到需要判断是否已经签到请假
    public function _onAdd($myUid)
    {
        $opt = $this->option();
        // 获取认证编号
        $user = (new User())->isCache(true)->oneFind(['id' => $myUid]);
        if(empty($user)){
            return $this->_returnFail('你选择的用户不存在！');
        }
        if(empty($user['is_auth'])){
            return $this->_returnFail('你不是认证用户！');
        }
        if($user['verify'] != 1){
            return $this->_returnFail('你选择的用户认证未通过！');
        }


        $data = [
            //'id'         => $opt->getData("id", null, "intval"),
            'uid'        => $opt->getData("uid", null, "intval"),
            'course_id'  => $opt->getData("course_id", null, "intval"),
            'time_id'    => $opt->getData("time_id", null, "intval"),
            'student_id' => $opt->getData("student_id", null, "intval"),
            'course_uid' => $opt->getData("course_uid", null, "intval"),
            'up_time'    => $opt->getData("up_time", null, "intval"),
            'status'     => $opt->getData("status", null, "trim"),
            'is_up'      => $opt->getData("is_up", null, "trim"),
            //'ctime'      => $opt->getData("ctime", null, "intval"),
            //'mtime'      => $opt->getData("mtime", null, "intval"),
        ];


        $data = $this->filterNull($data);
        if(empty($data)){
            return $this->_returnFail('请添加需要保存的数据！');
        }

        // 组合数据
        $model = new CourseSign();
        $data  = $model->checkData($data);
        if($data === false){
            return $this->_returnFail('请添加需要保存的数据！');
        }

        // 验证
        $result = $model->checkRules($data, 'add');
        if($result === false){
            return $this->_returnFail($model->getError());
        }

        // 保存
        $result = $model->oneAdd($data);
        if($result === false){
            return $this->_returnFail($model->getError());
        }

        return $result;
    }

    // 批量签到
    // TODO 签到需要判断是否已经签到请假
    public function _batch($myUid)
    {
        $opt = $this->option();
        // 获取认证编号
        $user = (new User())->isCache(true)->oneFind(['id' => $myUid]);
        if(empty($user)){
            return $this->_returnFail('你选择的用户不存在！');
        }
        if(empty($user['is_auth'])){
            return $this->_returnFail('你不是认证用户！');
        }
        if($user['verify'] != 1){
            return $this->_returnFail('你选择的用户认证未通过！');
        }

        // 数据
        $data = [
            //'id'         => $opt->getData("id", null, "intval"),
            //'uid'        => $opt->getData("uid", null, "intval"),
            'uid'        => $myUid,
            'course_id'  => $opt->getData("course_id", null, "intval"),
            'time_id'    => $opt->getData("time_id", null, "intval"),
            'student_id' => $opt->getData("student_id", [], "is_array"),
            'course_uid' => $opt->getData("course_uid", [], "is_array"),
            'up_time'    => $opt->getData("up_time", null, "intval"),
            'status'     => $opt->getData("status", null, "trim"),
            'is_up'      => $opt->getData("is_up", null, "trim"),
            //'ctime'      => $opt->getData("ctime", null, "intval"),
            //'mtime'      => $opt->getData("mtime", null, "intval"),
        ];


        $data = $this->filterNull($data);
        if(empty($data)){
            return $this->_returnFail('请添加需要保存的数据！');
        }

        // 验证
        $model  = new CourseSign();
        $result = $model->checkRules($data, 'add');
        if($result === false){
            return $this->_returnFail($model->getError());
        }

        // 整理数据
        $uids = $data['student_id'];
        $stus = (new Student())->isCache(true)->getListInIds($uids);
        $row  = $data;
        $list = [];
        unset($row['student_id']);
        foreach($uids as $_uid){
            $row['student_id'] = $_uid;
            $row['course_uid'] = empty($stus[$_uid]) ? 0 : $stus[$_uid]['person_uid'];
            // 组合
            $list[] = $row;
        }

        // 保存数据
        $result = $model->insert($list);
        if($result === false){
            return $this->_returnFail($model->getError());
        }

        // 清理缓存
        $model->deleteCacheList($list);

        return $result;
    }


}


